using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Application.Dtos;

namespace CKY.AgentPlatform.Users.Dtos
{
    /// <summary>
    /// 用户登录DTO
    /// </summary>
    public class UserLoginDto
    {
        /// <summary>
        /// 用户名或邮箱
        /// </summary>
        [Required(ErrorMessage = "用户名或邮箱不能为空")]
        [StringLength(256, ErrorMessage = "用户名或邮箱长度不能超过256个字符")]
        public string UsernameOrEmail { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Required(ErrorMessage = "密码不能为空")]
        [StringLength(128, MinimumLength = 6, ErrorMessage = "密码长度必须在6-128个字符之间")]
        public string Password { get; set; }

        /// <summary>
        /// 是否记住我
        /// </summary>
        public bool RememberMe { get; set; } = false;

        /// <summary>
        /// 验证码（如果需要）
        /// </summary>
        public string Captcha { get; set; }
    }

    /// <summary>
    /// 用户注册DTO
    /// </summary>
    public class UserRegisterDto
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [Required(ErrorMessage = "用户名不能为空")]
        [StringLength(64, MinimumLength = 3, ErrorMessage = "用户名长度必须在3-64个字符之间")]
        [RegularExpression("^[a-zA-Z0-9_]+$", ErrorMessage = "用户名只能包含字母、数字和下划线")]
        public string UserName { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [Required(ErrorMessage = "邮箱不能为空")]
        [EmailAddress(ErrorMessage = "邮箱格式不正确")]
        [StringLength(256, ErrorMessage = "邮箱长度不能超过256个字符")]
        public string Email { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Required(ErrorMessage = "密码不能为空")]
        [StringLength(128, MinimumLength = 8, ErrorMessage = "密码长度必须在8-128个字符之间")]
        [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$", 
            ErrorMessage = "密码必须包含至少1个大写字母、1个小写字母、1个数字和1个特殊字符")]
        public string Password { get; set; }

        /// <summary>
        /// 确认密码
        /// </summary>
        [Required(ErrorMessage = "确认密码不能为空")]
        [Compare("Password", ErrorMessage = "两次输入的密码不一致")]
        public string ConfirmPassword { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [Required(ErrorMessage = "姓名不能为空")]
        [StringLength(64, ErrorMessage = "姓名长度不能超过64个字符")]
        public string Name { get; set; }

        /// <summary>
        /// 姓氏
        /// </summary>
        [Required(ErrorMessage = "姓氏不能为空")]
        [StringLength(64, ErrorMessage = "姓氏长度不能超过64个字符")]
        public string Surname { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        [Phone(ErrorMessage = "电话号码格式不正确")]
        [StringLength(32, ErrorMessage = "电话号码长度不能超过32个字符")]
        public string PhoneNumber { get; set; }

        /// <summary>
        /// 部门
        /// </summary>
        [StringLength(128, ErrorMessage = "部门长度不能超过128个字符")]
        public string Department { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        [StringLength(128, ErrorMessage = "职位长度不能超过128个字符")]
        public string Position { get; set; }
    }

    /// <summary>
    /// 用户认证结果DTO
    /// </summary>
    public class UserAuthResultDto
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 访问令牌
        /// </summary>
        public string AccessToken { get; set; }

        /// <summary>
        /// 刷新令牌
        /// </summary>
        public string RefreshToken { get; set; }

        /// <summary>
        /// 令牌类型
        /// </summary>
        public string TokenType { get; set; } = "Bearer";

        /// <summary>
        /// 过期时间（秒）
        /// </summary>
        public int ExpiresIn { get; set; }

        /// <summary>
        /// 用户信息
        /// </summary>
        public UserInfoDto User { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; set; }
    }

    /// <summary>
    /// 用户信息DTO
    /// </summary>
    public class UserInfoDto : EntityDto<Guid>
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 姓氏
        /// </summary>
        public string Surname { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        public string PhoneNumber { get; set; }

        /// <summary>
        /// 头像URL
        /// </summary>
        public string AvatarUrl { get; set; }

        /// <summary>
        /// 部门
        /// </summary>
        public string Department { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        public string Position { get; set; }

        /// <summary>
        /// 是否邮箱已确认
        /// </summary>
        public bool EmailConfirmed { get; set; }

        /// <summary>
        /// 是否电话已确认
        /// </summary>
        public bool PhoneNumberConfirmed { get; set; }

        /// <summary>
        /// 是否启用双因子认证
        /// </summary>
        public bool TwoFactorEnabled { get; set; }

        /// <summary>
        /// 是否管理员
        /// </summary>
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        public DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 角色列表
        /// </summary>
        public List<string> Roles { get; set; } = new List<string>();

        /// <summary>
        /// 权限列表
        /// </summary>
        public List<string> Permissions { get; set; } = new List<string>();
    }

    /// <summary>
    /// 修改密码DTO
    /// </summary>
    public class ChangePasswordDto
    {
        /// <summary>
        /// 当前密码
        /// </summary>
        [Required(ErrorMessage = "当前密码不能为空")]
        public string CurrentPassword { get; set; }

        /// <summary>
        /// 新密码
        /// </summary>
        [Required(ErrorMessage = "新密码不能为空")]
        [StringLength(128, MinimumLength = 8, ErrorMessage = "新密码长度必须在8-128个字符之间")]
        [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$", 
            ErrorMessage = "新密码必须包含至少1个大写字母、1个小写字母、1个数字和1个特殊字符")]
        public string NewPassword { get; set; }

        /// <summary>
        /// 确认新密码
        /// </summary>
        [Required(ErrorMessage = "确认新密码不能为空")]
        [Compare("NewPassword", ErrorMessage = "两次输入的新密码不一致")]
        public string ConfirmNewPassword { get; set; }
    }

    /// <summary>
    /// 重置密码DTO
    /// </summary>
    public class ResetPasswordDto
    {
        /// <summary>
        /// 用户名或邮箱
        /// </summary>
        [Required(ErrorMessage = "用户名或邮箱不能为空")]
        public string UsernameOrEmail { get; set; }

        /// <summary>
        /// 重置令牌
        /// </summary>
        [Required(ErrorMessage = "重置令牌不能为空")]
        public string ResetToken { get; set; }

        /// <summary>
        /// 新密码
        /// </summary>
        [Required(ErrorMessage = "新密码不能为空")]
        [StringLength(128, MinimumLength = 8, ErrorMessage = "新密码长度必须在8-128个字符之间")]
        [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$", 
            ErrorMessage = "新密码必须包含至少1个大写字母、1个小写字母、1个数字和1个特殊字符")]
        public string NewPassword { get; set; }

        /// <summary>
        /// 确认新密码
        /// </summary>
        [Required(ErrorMessage = "确认新密码不能为空")]
        [Compare("NewPassword", ErrorMessage = "两次输入的新密码不一致")]
        public string ConfirmNewPassword { get; set; }
    }

    /// <summary>
    /// 忘记密码DTO
    /// </summary>
    public class ForgotPasswordDto
    {
        /// <summary>
        /// 邮箱
        /// </summary>
        [Required(ErrorMessage = "邮箱不能为空")]
        [EmailAddress(ErrorMessage = "邮箱格式不正确")]
        public string Email { get; set; }
    }

    /// <summary>
    /// 刷新令牌DTO
    /// </summary>
    public class RefreshTokenDto
    {
        /// <summary>
        /// 刷新令牌
        /// </summary>
        [Required(ErrorMessage = "刷新令牌不能为空")]
        public string RefreshToken { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }
    }
}